package com.pig.controller;

import com.pig.pojo.user.User;
import com.pig.service.UserService;
import com.pig.util.SysResultLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 分页查询用户信息
     */
    @RequestMapping("/user/findUserByPage")
    public SysResultLayui findItemByPage(int page, int limit){
        return userService.findUserByPage(page,limit);
    }
    /**添加用户信息*/
    @RequestMapping("/user/save")
    public SysResultLayui<User> saveUser(User user){
        //防止系统自动提交
        User user1 = userService.findByUserName(user.getUsername());
        if(null != user1){
            return new SysResultLayui<>(201,"用户名已存在，请重新输入！",1,null);
        }
        try {
            userService.saveUsername(user);
            return SysResultLayui.success();
        }catch (Exception e){
            return SysResultLayui.fail();
        }
    }
    /**判断用户名是否存在*/
    @RequestMapping("checkUserByUsername/{username}")
    public SysResultLayui<User> checkUserByUsername(@PathVariable("username") String username){
        User user = userService.findByUserName(username);
        if(user != null){
            return new SysResultLayui<>(201,"用户名已存在，请重新输入！",1,null);
        }
        return SysResultLayui.success();

    }
    /**修改用户信息*/
    @RequestMapping("/user/update")
    public SysResultLayui<User> updateUser(User user){
        System.out.println(user.getId());
        userService.updateUsers(user);
        return new SysResultLayui<>();
    }

    /**删除用户信息*/
    @RequestMapping("/user/delete")
    public SysResultLayui<User> deleteUser(Long id){
        System.out.println(id);
        userService.deleteUser(id);
        return new SysResultLayui<>();
    }
    /**修改密码*/
    @RequestMapping("/user/updatepassword")
    public SysResultLayui<User> updatePassword(HttpServletRequest request){
        Integer id = Integer.valueOf(request.getParameter("id"));
        System.out.println(id);
        String oldpass = request.getParameter("oldpass");
        String newpass = request.getParameter("newpass");
        if(id != null) {
            userService.updatepassword(id, oldpass, newpass);
        }else {
            return SysResultLayui.fail();
        }
        return SysResultLayui.success();
    }
    /**根据用户名查询用户信息*/
    @RequestMapping("/user/findUserByName")
    public SysResultLayui<User> findUserByUserName(@PathVariable("username") String username){
        return userService.findUserByUserName(username);
    }
    /**根据id改变用户状态*/
    @RequestMapping("/user/updateUserStatusById")
    public SysResultLayui<User> updateUserStatusById(int id ,int status){
        return userService.updateUserStatusById(id,status);
    }
}
